﻿using System.Web.Mvc;
using INSE6260.OnlineBanking.Model.ClientCard;
using INSE6260.OnlineBanking.Model.Clients;
using INSE6260.OnlineBanking.Web.MVC.Helpers;

namespace INSE6260.OnlineBanking.Web.MVC.Controllers
{
    public class LoginController : Controller
    {
        private readonly IClientCardRepository _cardRepository;
        public LoginController(IClientCardRepository cardRepository)
        {
            _cardRepository = cardRepository;
        }

        #region Login

        public ActionResult Login()
        {

            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Login(LoginInfo loginInfo)
        {
            if (ModelState.IsValid)
            {
                if (_cardRepository.ExistCardNo(loginInfo))
                {
                    if (_cardRepository.IsLocked(loginInfo))
                    {
                        _cardRepository.IsSecured(loginInfo);
                        if (UserProvider.ActiveClient == null)
                        {
                            UserProvider.ActiveClient = new ClientToken {Counter = loginInfo.Counter};
                        }
                        var account = _cardRepository.IsValid(loginInfo);
                        if (UserProvider.ActiveClient.Counter <= 3 && account != null)
                        {

                            var seccurityInfo = new SecurityInfo
                                                    {
                                                        Account = account,
                                                        AccountId = account.Id,
                                                        Question =
                                                            _cardRepository.GetRandomSecurityQuestion(
                                                                _cardRepository.GetClientCard(account.Id))
                                                    };
                            UserProvider.ActiveClient.Counter = 0;
                            return View("Security", seccurityInfo);
                        }
                        UserProvider.ActiveClient.Counter = UserProvider.ActiveClient.Counter - 1;

                        if (UserProvider.ActiveClient.Counter > 0)
                        {
                            ModelState.AddModelError("",
                                                     "Wrogn Card Number or Password! you can try it again only " +
                                                     UserProvider.ActiveClient.Counter.ToString() + " time(s)");
                            return View(loginInfo);
                        }
                        if (UserProvider.ActiveClient.Counter == 0)
                        {
                            var card = _cardRepository.GetClientCard(loginInfo.CardNo);
                            _cardRepository.BlockCard(card.Id);
                            return View("ErrorPage");
                        }
                        ModelState.AddModelError("", "Wrogn Card Number or Password");
                        return View();
                    }
                    ModelState.AddModelError("", "Your card is locked, please call the adminitrator");
                    return View();
                }
                ModelState.AddModelError("", "This card No doesn't exist");
                return View();
            }
            ModelState.AddModelError("", "Wrogn Card Number or Password");
            return View();

        }
        #endregion

        #region Security
        public ActionResult Security()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Security(SecurityInfo securityInfo)
        {
            if (ModelState.IsValid)
            {
                _cardRepository.IsSecured(securityInfo);
                //if (UserProvider.ActiveClient == null)
                //{
                //    UserProvider.ActiveClient = new ClientToken { Counter = securityInfo.Counter };
                //}
                if (securityInfo.IsSecuredToken)
                {
                    var clientToken = new ClientToken
                                          {
                                              ClientId = securityInfo.ClientId,
                                              FirstName = securityInfo.Client.FirstName,
                                              LastName = securityInfo.Client.LastName,
                                              CardNo = securityInfo.CardNo,
                                              Password = securityInfo.Password,
                                              ClientCardId = securityInfo.ClientCardId,
                                              Counter = 0
                                          };

                    UserProvider.ActiveClient = clientToken;
                    return RedirectToAction("Index", "Home", new { clientId = securityInfo.ClientId });
                }
                var card = _cardRepository.GetClientCard(securityInfo.CardNo, securityInfo.Password);
                securityInfo.Question = _cardRepository.GetRandomSecurityQuestion(card);
                securityInfo.Answer = string.Empty;
                ModelState.AddModelError("", "Your challenge answer was invalid, please try again!");
            }
            return View(securityInfo);
        }
        #endregion

        #region Logout
        public ActionResult LogOut()
        {
            UserProvider.ActiveClient = null;
            return RedirectToAction("Login");
        }

        #endregion
    }
}